package com.wanhe.shoppingcart.counter;

import android.text.TextUtils;

import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.holder.DishHolder;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.List;

/**
 * Created by XieZaiWei on 2017/5/9.
 */

public class OnlyOneExistCounter extends BaseCustomCounter implements ICounter {
	public static final String COUNTER_TYPE = "only_one";

	public OnlyOneExistCounter(BaseCounter counter) {
		super(counter);
	}

	@Override
	public String getCounterType() {
		return COUNTER_TYPE;
	}

	@Override
	public void plus(String foodID, boolean isNeedSyncToServer) {
		//这里做的操作，就是某个页面中有些菜类在购物车中只能存在一个的情况下的逻辑
		Dish dish = DishHolder.getInstance().get(foodID);
		if (ObjectUtil.checkObject(dish)) {
			String type = dish.getTxt1();
			String dish_type_guid = dish.dish_type_guid;
			List<ICartItem> cartItems = CartManager.getInstance().getTotalCartItem();

			for (int i = 0; i < cartItems.size(); i++) {
				CartItem cartItem = (CartItem) cartItems.get(i);
				String tempType = cartItem.getDish().getTxt1();
				//判断购物车中是否有该类型的菜品，如果有，那么删掉，如果没有直接添加到购物车
				if (type.equals(tempType)) {
					CartManager.getInstance().deleteItem(cartItem);
					break;
				}
			}
			mBaseCounter.plus(foodID, isNeedSyncToServer);

		}
	}

	@Override
	public void plus(ICartItem item, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(item.getDishGuid());
		if (ObjectUtil.checkObject(dish)) {
			String type = dish.getTxt1();
			List<ICartItem> cartItems = CartManager.getInstance().getTotalCartItem();
			CartItem deleteTempItem = null;
			for (int i = 0; i < cartItems.size(); i++) {
				CartItem cartItem = (CartItem) cartItems.get(i);
				String tempType = cartItem.getDish().getTxt1();
				//判断购物车中是否有该类型的菜品，如果有，那么删掉，如果没有直接添加到购物车
				if (type.equals(tempType)) {
					deleteTempItem = cartItem;
					break;
				}
			}
			if (ObjectUtil.checkObject(deleteTempItem)) {
				String tempGuid = deleteTempItem.getDish().getDish_guid();
				if (!tempGuid.equals(dish.getDish_guid())) {
					CartManager.getInstance().deleteItem(deleteTempItem);
					this.mBaseCounter.plus(item.getDishGuid(), isNeedSyncToServer);
				}
			} else {
				this.mBaseCounter.plus(item.getDishGuid(), isNeedSyncToServer);
			}
		}
	}

	@Override
	public void reduce(String foodID, boolean isNeedSyncToServer) {
		mBaseCounter.reduce(foodID, isNeedSyncToServer);
	}

	@Override
	public void reduce(ICartItem item, boolean isNeedSyncToServer) {
		mBaseCounter.reduce(item.getDishGuid(), isNeedSyncToServer);
	}


	@Override
	public boolean isCountFilled() {
		int pingguoCount = 0;
		List<ICartItem> cartItems = CartManager.getInstance().getTotalCartItem();
		for (int i = 0; i < cartItems.size(); i++) {
			CartItem cartItem = (CartItem) cartItems.get(i);
			String type = cartItem.getDish().getTxt1();
			if (!TextUtils.isEmpty(type) && type.contains(CounterConfig.GIF_ARR_DISH_TYPE)) {
				pingguoCount++;
			}
		}
		// 这里临时添加对拼锅的检测，如果没有两个，那么下单不能执行
		boolean result = true;
		if (pingguoCount >= 1 && pingguoCount < 2) {
//			CounterManager.getInstance().err = "拼锅不能只点半边！";
			result = false;
		}
		return result;
	}
}
